package com.huanxing.cloud.pay.api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.pay.api.handler.PayOrderHandler;
import com.huanxing.cloud.pay.api.service.IPayTradeOrderService;
import com.huanxing.cloud.pay.common.dto.CreateOrderRequest;
import com.huanxing.cloud.pay.common.entity.PayTradeOrder;
import com.huanxing.cloud.security.annotation.HxInner;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/pay")
@RequiredArgsConstructor
public class PayOrderController {

	private final Map<String, PayOrderHandler> payOrderHandlerMap;

	private final IPayTradeOrderService payTradeOrderService;

	@HxInner
	@ApiOperation(value = "创建订单")
	@PostMapping("/create-order")
	public Result create(@RequestBody CreateOrderRequest createOrderRequest) {
		return payOrderHandlerMap.get(createOrderRequest.getTradeType()).pay(createOrderRequest);
	}

	@ApiOperation(value = "通过id查询")
	@SaCheckPermission("payapi:paytradeorder:get")
	@GetMapping("/{id}")
	public Result getById(@PathVariable("id") String id) {
		return Result.success(payTradeOrderService.getById(id));
	}

	@ApiOperation(value = "分页查询")
	@SaCheckPermission("payapi:paytradeorder:page")
	@GetMapping("/page")
	public Result getPage(Page page, PayTradeOrder payTradeOrder) {
		return Result.success(payTradeOrderService.page(page, Wrappers.query(payTradeOrder)));
	}

}
